package com.etoak.controller;

import com.etoak.common.utils.uuid.IdUtils;
import com.etoak.common.vo.ResultVO;
import com.etoak.entity.Label;
import com.etoak.service.LabelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hnb
 * @version 1.0
 * @Description:
 * @date 2022/5/10 下午5:13
 */
@RestController
@RequestMapping("/goertek/label")
public class LabelController extends BaseController {
    @Autowired
    LabelService labelService;

    /**
     * 添加标签
     * @param label
     * @return
     */
    @PostMapping("/add")
    public ResultVO add(@RequestBody Label label) {
        label.setId(IdUtils.simpleUUID());
        int i = labelService.add(label);
        if (i != 0) {
            return ResultVO.success(true);
        }
        return ResultVO.error(false);
    }

    @PostMapping("/update")
    public ResultVO update(@RequestBody Label label){
        labelService.updateLabel(label);
        return  ResultVO.success("修改成功");
    }
    @GetMapping("/query")
    public ResultVO query(Label label){
        Label lab = labelService.query(label);
        return ResultVO.success(lab);
    }

    /**
     * 查询标签列表
     * @param label
     * @return
     */
    @GetMapping("/queryList")
    public ResultVO queryList(Label label){
        List<Label> list = labelService.queryList(label);
        return ResultVO.success(list);
    }

    /**
     * 查询有效标签
     * @return
     */
    @GetMapping("/queryValidLabel")
    public ResultVO queryValidLabel(){
        Label label=new Label();
        label.setStatus("0");
        List<Label> list = labelService.queryList(label);
        return ResultVO.success(list);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    public ResultVO queryById(@PathVariable("id") String id){
        Label label = new Label();
        label.setId(id);
        Label lab = labelService.query(label);
        return ResultVO.success(lab);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable("ids") String[] ids){
        return toAjax(labelService.deletelabel(ids));
    }
    /**
     * 逻辑删除
     * @param ids
     * @return
     */
    @GetMapping("/delete/{ids}")
    public AjaxResult remove2(@PathVariable("ids") String[] ids){
        int i = 0;
        for(String id : ids){
            Label label1 = new Label();
            label1.setId(id);
            Label label = labelService.query(label1);
            label.setDelStatus("1");
            labelService.updateLabel(label);
            i++;
        }
        return AjaxResult.success("已删除"+ i +"条记录",200);
    }

}
